package com.syb.controller.function;


import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.syb.entity.function.SysResource;
import com.syb.service.ResourceService;
import com.syb.util.PageResults;

@Namespace("/")
public class ResourcesController extends ActionSupport{

	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger(ResourcesController.class);
	
	@Autowired
	private ResourceService resouceService;
	

	private Integer currentPage;
	
	private String name;
	
	private String resorceName;

	private Integer parentId;
	
	private String resorceUrl;
	
	private Integer id;
	
	@Action(value="/resources/getResorcesList",results={
			@Result(name="success",location="/WEB-INF/page/resource/list.jsp"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String getResoucesList(){
		PageResults<SysResource> resourceList = new PageResults<SysResource>();
		PageResults<SysResource> page = new PageResults<SysResource>();
		if(name!=null&&name.length()>0){
			page.getParamMap().put("name", name);
		}
		if(currentPage!=null&&!currentPage.equals("")){
			page.setCurrentPage(currentPage);
		}
		try{
			resourceList = resouceService.findSysResourcesList(page);
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error", e.getMessage());
			return "error";
		}
		ActionContext.getContext().put("result", resourceList);
		return "success";
	}

	
	//跳转到添加页面
	@Action(value="/resources/addResorces",results={
			@Result(name="success",location="/WEB-INF/page/resource/add.jsp"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String addResorces(){
		//查询所有一级菜单目前只有两级菜单
		List<SysResource> sysResources = new ArrayList<SysResource>();
		try{
			sysResources = resouceService.findResourcesDing();
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error", e.getMessage());
			return "error";
		}
		ActionContext.getContext().put("result",sysResources);
		return "success";
	}
	
	//添加资源
	@Action(value="/resources/insertResorces",results={
			@Result(name="success",type="redirect",location="/resources/getResorcesList"),
			@Result(name="error",location="/WEB-INF/page/resource/add.jsp"),
			@Result(name="exce",location="/WEB-INF/fail.jsp")
			})
	public String insertResorces(){
		SysResource sys = new SysResource();
		if(parentId.equals(0)){
			sys.setText(parentId.toString());
		}else{
			sys.setParentId(parentId);
		}
		sys.setResourceName(resorceName);
		sys.setResourceUrl(resorceUrl);
		//下拉列表需要的值
		List<SysResource> sysResources = new ArrayList<SysResource>();
		try{
			sysResources = resouceService.findResourcesDing();
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error", e.getMessage());
			return "exce";
		}
		
		try{
			resouceService.insertResource(sys);
		}catch(Exception e){
			//查询所有一级菜单目前只有两级菜单
			ActionContext.getContext().put("result",sysResources);
			logger.error(e.getMessage());
			ActionContext.getContext().put("error", e.getMessage());
			ActionContext.getContext().put("parentId", parentId);
			ActionContext.getContext().put("resorceName", resorceName);
			ActionContext.getContext().put("resorceUrl", resorceUrl);
			return "error";
		}
		
		return "success";
	}

	//删除资源
	@Action(value="/resources/delResorces",results={
			@Result(name="success",type="redirect",location="/resources/getResorcesList"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String delResorces(){
		SysResource sys = new SysResource();
		sys.setId(id);
		try{
			resouceService.delResource(sys);
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error", e.getMessage());
			return "error";
		}
		
		return "success";
	}	

	//查看资源详细信息
	@Action(value="/resources/detailResorces",results={
			@Result(name="success",location="/WEB-INF/page/resource/detail.jsp"),
			@Result(name="error",location="/WEB-INF/fail.jsp")
			})
	public String detailResorces(){
		List<SysResource> sysList = new ArrayList<SysResource>();
		//下拉列表需要的值
		List<SysResource> sysResources = new ArrayList<SysResource>();
		try{
			sysResources = resouceService.findResourcesDing();
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error", e.getMessage());
			return "error";
		}
		ActionContext.getContext().put("result",sysResources);
		//根据id查询资源详情
		SysResource sys = new SysResource();
		sys.setId(id);
		try{
			sysList = resouceService.findResourceByName(sys);
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error", e.getMessage());
			return "error";
		}
		if(sysList.size()>0){
			ActionContext.getContext().put("resources", sysList.get(0));
		}
		return "success";
	}
	

	//修改资源跳转页面
	@Action(value="/resources/updateResorces",results={
			@Result(name="success",location="/WEB-INF/page/resource/update.jsp"),
			@Result(name="error",location="/WEB-INF/page/resource/update.jsp"),
			@Result(name="exce",location="/WEB-INF/fail.jsp")
			})
	public String updateResorces(){
		List<SysResource> sysList = new ArrayList<SysResource>();
		//下拉列表需要的值
		List<SysResource> sysResources = new ArrayList<SysResource>();
		try{
			sysResources = resouceService.findResourcesDing();
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error", e.getMessage());
			return "error";
		}
		ActionContext.getContext().put("result",sysResources);
		//根据id查询资源详情
		SysResource sys = new SysResource();
		sys.setId(id);
		try{
			sysList = resouceService.findResourceByName(sys);
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error", e.getMessage());
			return "error";
		}
		if(sysList.size()>0){
			ActionContext.getContext().put("resources", sysList.get(0));
		}
		return "success";
	}	
	
	//修改资源
	@Action(value="/resources/updateSysResorces",results={
			@Result(name="success",type="redirect",location="/resources/getResorcesList"),
			@Result(name="error",location="/WEB-INF/page/resource/update.jsp"),
			@Result(name="exce",location="/WEB-INF/fail.jsp")
			})
	public String updateSysResorces(){
		//下拉列表需要的值
		List<SysResource> sysResources = new ArrayList<SysResource>();
		try{
			sysResources = resouceService.findResourcesDing();
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error", e.getMessage());
			return "error";
		}
		ActionContext.getContext().put("result",sysResources);
		//更新
		SysResource sys = new SysResource();
		if(parentId==null ||parentId.equals(0)){
			sys.setText("0");
		}else{
			sys.setParentId(parentId);
		}
		sys.setId(id);
		sys.setResourceName(resorceName);
		sys.setResourceUrl(resorceUrl);
		try{
		 resouceService.updateResource(sys);
		}catch(Exception e){
			logger.error(e.getMessage());
			ActionContext.getContext().put("error", e.getMessage());
			return "error";
		}
		return "success";
	}	
	
	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	public String getResorceName() {
		return resorceName;
	}


	public void setResorceName(String resorceName) {
		this.resorceName = resorceName;
	}


	public Integer getParentId() {
		return parentId;
	}


	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}


	public String getResorceUrl() {
		return resorceUrl;
	}


	public void setResorceUrl(String resorceUrl) {
		this.resorceUrl = resorceUrl;
	}


	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}
	
	
}
